package com.ternnetwork.configuration.service.impl.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.configuration.dao.job.QuartzJobDao;
import com.ternnetwork.configuration.dao.job.QuartzJobTriggerDao;
import com.ternnetwork.configuration.entity.job.QuartzJob;
import com.ternnetwork.configuration.entity.job.QuartzJobTrigger;
import com.ternnetwork.configuration.service.job.SystemJobService;
import com.ternnetwork.configuration.service.job.QuartzJobService;
import com.ternnetwork.configuration.ui.QuartzJobZtree;




@Service("quartzJobService")
public class QuartzJobServiceImpl implements QuartzJobService {
	
	
	@Resource
	private QuartzJobDao quartzJobDao;
	
	@Resource
	private QuartzJobTriggerDao quartzJobTriggerDao;

	@Resource
	private SystemJobService systemJobService;
	
	@Override
	public BaseResponse idoAdd(QuartzJob t){
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getGroup())){
			res.setStatus(1);
			res.setMessage("操作失败，分组不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getClassName())){
			res.setStatus(2);
			res.setMessage("操作失败，实现类不能为空！");
			return res;
		}
		Long totalCount=quartzJobDao.getTotalCount("select count(t.id) from QuartzJob t where t.name=?1", t.getName());
		if(totalCount.longValue()>0L){
			res.setStatus(3);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
		quartzJobDao.persist(t);
		res.setStatus(4);
		res.setMessage("操作成功！");
		return res;
	}
	
	
	@Override
	public BaseResponse idoUpdate(QuartzJob t) throws Exception{
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getGroup())){
			res.setStatus(1);
			res.setMessage("操作失败，分组不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getClassName())){
			res.setStatus(2);
			res.setMessage("操作失败，实现类不能为空！");
			return res;
		}
		Long totalCount=quartzJobDao.getTotalCount("select count(t.id) from QuartzJob t where t.name=?1 and t.id!=?2", t.getName(),t.getId());
		
		if(totalCount.longValue()>0L){
			res.setStatus(3);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
		
		QuartzJob quartzJob=quartzJobDao.findById(QuartzJob.class, t.getId());
		quartzJobDao.saveOrUpdate(t);
		List<QuartzJobTrigger> quartzJobTriggerList=quartzJobTriggerDao.findAll("from QuartzJobTrigger where job.id=?1",t.getId());
		for (QuartzJobTrigger quartzJobTrigger:quartzJobTriggerList){
			t.getTriggers().add(quartzJobTrigger);
		}
		if(t.getEnable()==true){
			if(t.getTriggers().size()>0){
				systemJobService.deleteJob(quartzJob);
				systemJobService.startJob(t);
			}
		}else{
		     systemJobService.deleteJob(quartzJob);
		}
		res.setStatus(4);
		res.setMessage("操作成功！");
		return res;
	}
	@Override
	public BaseResponse idoDelete(Long id)throws Exception{
		BaseResponse res=new BaseResponse();
		QuartzJob t=quartzJobDao.findById(QuartzJob.class, id);
		if(t==null){
			res.setStatus(0);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
		quartzJobDao.delete(t);
		systemJobService.deleteJob(t);
		res.setStatus(1);
		res.setMessage("操作成功！");
		return res;
	}
	@Override
	public List<QuartzJobZtree>getZtreeList(){
		
		List<QuartzJobZtree> retVal=new ArrayList<QuartzJobZtree>();
		
		List<QuartzJob> jobList=quartzJobDao.findAll("from QuartzJob");
		
		for(QuartzJob quartzJob:jobList){
			
			QuartzJobZtree tree=new QuartzJobZtree();
			tree.setClassName(quartzJob.getClassName());
			tree.setName(quartzJob.getName());
            tree.setGroup(quartzJob.getGroup());	
            tree.setEnable(quartzJob.getEnable());
            tree.setId("J_"+quartzJob.getId());
            tree.setParentId(0L);
    		tree.setRealId(quartzJob.getId());
    		tree.setIsParent(true);
            retVal.add(tree);
            Set<QuartzJobTrigger>  quartzJobTriggers=quartzJob.getTriggers();
   	        for(QuartzJobTrigger quartzJobTrigger:quartzJobTriggers){
	 			QuartzJobZtree triggerTree=new QuartzJobZtree();
	 			triggerTree.setName(quartzJobTrigger.getName());
	 			triggerTree.setGroup(quartzJobTrigger.getGroup());	
	 			triggerTree.setCronExpression(quartzJobTrigger.getCronExpression());
	 			triggerTree.setId("T_"+quartzJobTrigger.getId());
	 			triggerTree.setpId(tree.getId());
	 			triggerTree.setParentId(tree.getRealId());
	 			triggerTree.setRealId(quartzJobTrigger.getId());
	 			triggerTree.setIsParent(false);
	            retVal.add(triggerTree);
            }
		}
		return retVal;
	}
	
	
	
	
}
